// $Id: privatemsg.js,v 1.3.2.4 2008/06/13 05:11:48 litwol Exp $

function _update_view() {
  $('.delete-container').each( function(i, o) {
    $('#delete-container-' + i).slideUp('slow');
  });
  $('.reply-container').each( function(i, o) {
    $('#reply-container-' + i).slideUp('slow');
  });

  $('.forward-container').each( function(i, o) {
    $('#forward-container-' + i).slideUp('slow');
  });
  $('.attachments-container').each( function(i, o) {
    $('#attachments-container-' + i).slideUp('fast');
  });
  $('#forwardall-container').slideUp('slow');
 
}

/**
 * note: some error when do action with no messages
 * 
 * @param {Object}
 *          '.js-hide'
 */
if (Drupal.jsEnabled) {
  $(document).ready( function() {
    // Hide the elements we want hidden with JS.
      $('.js-hide').hide();

      // Show the elements we want to expose with JS.
      $('.js-show').show();

      // Add the JS to the folder select if necessary.
      $('.mail-action-select').change( function() {
        // click is necessary for right $op.
          $('#edit-do-action').each( function() {
            this.click();
          });
        });

      $('.mail-per-page-select').change( function() {
        // click is necessary for right $op.
          $('#edit--refresh').each( function() {
            this.click();
          });
        });

      $('.container-inline.has-reply-message.collapsed').removeClass(
          'collapsed');

      $('.reply-container.has-reply-message').slideDown();

      $('.mail-filter-select').change(
          function() {
            var table = $('#sl_mail_message_table');
            switch (this.value) {
            case '_all':
              table.find('tr td input:checkbox').attr('checked', true);
              break;
            case '_none':
              table.find('tr td input:checkbox').attr('checked', false);
              break;
            case '_read':
              table.find('tr.mail-read td input:checkbox')
                  .attr('checked', true).end().find(
                      'tr:not(.mail-read) td input:checkbox').attr('checked',
                      false);
              break;
            case '_unread':
              table.find('tr.mail-read td input:checkbox').attr('checked',
                  false).end().find('tr:not(.mail-read) td input:checkbox')
                  .attr('checked', true);
              break;
            case '_starred':
              table.find('tr.mail-star td input:checkbox')
                  .attr('checked', true).end().find(
                      'tr:not(.mail-star) td input:checkbox').attr('checked',
                      false);
              break;
            case '_unstarred':
              table.find('tr.mail-star td input:checkbox').attr('checked',
                  false).end().find('tr:not(.mail-star) td input:checkbox')
                  .attr('checked', true);
              break;
            case '_default':
              break;
            default:
              break;
            }
            var all = true;
            table.find('td input:checkbox').each(
                function() {
                  all = all && this.checked;
                  $(this).parents('tr:first')[this.checked ? 'addClass'
                      : 'removeClass']('selected');
                });

            this.value = '_default';
          });

      $('.reply-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function(e) {
            // check whether this is a true click trigger
            if(e.clientX == 0 && e.clientY == 0)
              return false;
            _update_view();
            $('.select-user-error').hide(); 
            var display = $('#reply-container-' + i).css("display");
            if (display == 'block')
              $('#reply-container-' + i).slideUp();
            else if (display == 'none')
              $('#reply-container-' + i).slideDown();

            return false;
          });
        });

      $('#expand-collapse-link-wrapper').click(
          function() {
            var text = $('#expand-collapse-link-wrapper').text().toLowerCase();
            if (text.match(/expand all/)) {
              $('.container-inline.collapsed').removeClass('collapsed');
              $('#expand-collapse-link-wrapper').html(
                  '<a href="/statuslines/">Collapse All</a>');
            } else if (text.match(/collapse all/)) {
              $('#expand-collapse-link-wrapper').html(
                  '<a href="/statuslines/">Expand All</a>');
              $('.container-inline.viewmail').addClass('collapsed');
            }
            return false;
          });

      $('#compose-attachments-link').click( function() {
        var display = $('#compose-attachments').css("display");
        if (display == 'block')
          $('#compose-attachments').slideUp('fast');
        else if (display == 'none')
          $('#compose-attachments').slideDown('fast');
        return false;
      });

      $('.view-attachments-link').each( function(i, o) {
        $('#' + o.id).click( function() {
          var display = $('#view-attachments-' + i).css("display");
          if (display == 'block')
            $('#view-attachments-' + i).slideUp('fast');
          else if (display == 'none')
            $('#view-attachments-' + i).slideDown('fast');
          return false;
        });
      });

      $('.forward-link').each( function(i, o) {

        $('#' + o.id).click( function() {

          _update_view();
          var display = $('#forward-container-' + i).css("display");
          if (display == 'block')
            $('#forward-container-' + i).slideUp('slow');
          else if (display == 'none')
            $('#forward-container-' + i).slideDown('slow');
          return false;
        });
      });

      $('.forwardall-link').each( function(i, o) {
        $('#' + o.id).click( function(e) {
          // check whether this is a true click trigger
          if(e.clientX == 0 && e.clientY == 0)
            return false;
          
          _update_view();
          var display = $('#forwardall-container').css("display");
          if (display == 'block')
            $('#forwardall-container').slideUp('slow');
          else if (display == 'none')
            $('#forwardall-container').slideDown('slow');
          return false;
        });
      });

      $('.delete-link').each( function(i, o) {

        $('#' + o.id).click( function() {
          _update_view();
          var display = $('#delete-container-' + i).css("display");
          if (display == 'block')
            $('#delete-container-' + i).slideUp('fast');
          else if (display == 'none')
            $('#delete-container-' + i).slideDown('fast');
          return false;
        });
      });

      $('.reply-send-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#edit-action-type-' + i).val('reply');
            var n = $('#reply-container-' + i + ' input:checked').length;
            if (n) {
              $('#forward-container-' + i).empty();
              return true;
            } else {
              $('#select-user-error-' + i).show('fast');
              return false;
            }
          });
        });

      // hide error when click in select user check box
      $('.reply-container').each( function(i, o) {
        $('#' + o.id).find('.form-checkbox').each( function(j, s) {
          $('#' + s.id).click( function() {
            $('#select-user-error-' + i).hide('fast');
          });
        });
      });

      $('.forward-send-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#reply-container-' + i).empty();
            $('#edit-action-type-' + i).val('forward');
            return true;
          });
        });

      $('#forwardall-cancel-link').click( function() {
        $('#forwardall-container').slideUp('slow');
        return false;
      });

      $('.reply-save-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#forward-container-' + i).empty();
            $('#edit-action-type-' + i).val('reply');
            return true;
          });
        });

      $('.forward-save-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#reply-container-' + i).empty();
            $('#edit-action-type-' + i).val('forward');
            return true;
          });
        });

      $('.reply-cancel-link').each( function(i, o) {
        // alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#reply-container-' + i).slideUp('slow');
            return false;
          });
        });

      $('.forward-cancel-link').each( function(i, o) {
        //alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#forward-container-' + i).slideUp('slow');
            return false;
          });
        });

      $('.delete-yes-link').each( function(i, o) {
        //alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#reply-container-' + i).empty();
            $('#forward-container-' + i).empty();
            $('#edit-action-type-' + i).val('delete');
            return true;
          });
        });

      $('.delete-no-link').each( function(i, o) {
        //		alert('index: ' + i + ' object id: ' + o.id);
          $('#' + o.id).click( function() {
            $('#delete-container-' + i).slideUp('fast');
            return false;
          });
        });

      $('.reply-discard-link').each( function(i, o) {
        $('#' + o.id).click( function() {
          var number = o.id.match(/\d+$/);
          $('#edit-action-type-' + number).val('reply');
          return true;
        });
      });

      $('#compose-cancel-link').click( function() {
        url = top.location.toString();
        if (url.match(/compose\/edit\/\d+$/))
          top.location = '../../mailbox/draft';
        else if (url.match(/compose\/\d+$/))
          top.location = '../';
        else
          top.location = './';
        return false;
      });

      $('.select-user-error').hide();
      
    });
}
